package com.xzit.homework;

import org.w3c.dom.*;
import javax.xml.parsers.*;
import java.io.File;
import java.io.InputStream;
import java.lang.reflect.Method;

public class TestTeacherReflection {
    public static void main(String[] args) {
        try {
            // 读取 XML 文件
            InputStream is = TestTeacherReflection.class.getClassLoader().getResourceAsStream("teacher-config.xml");
            DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
            Document document = builder.parse(is);
            document.getDocumentElement().normalize();

            // 读取 <name> 和 <addr> 标签内容
            Element root = document.getDocumentElement();
            String name = root.getElementsByTagName("name").item(0).getTextContent();
            String addr = root.getElementsByTagName("addr").item(0).getTextContent();

            // 反射创建 Teacher 对象并赋值
            Class<?> clazz = Class.forName("com.xzit.homework.Teacher");
            Object teacher = clazz.getDeclaredConstructor().newInstance();

            clazz.getMethod("setName", String.class).invoke(teacher, name);
            clazz.getMethod("setAddr", String.class).invoke(teacher, addr);

            // 打印输出
            System.out.println(teacher);



        } catch (Exception e) {
            e.printStackTrace();
        }
    }

}
